(function() {
	module('Wooden blocks Tests');

	test('Setup OK', function() {
		ok(true);
	});

	test('Startup', function() {
		var view = new TestView();
		var controller = new WwallsController(view);

		controller.startup(new WBoard());

		ok(view.controller === controller);
	});

	test('Initial update', function() {
		var view = new TestView();
		var controller = new WwallsController(view);

		var board = new WBoard();
		board.blockBuilder().vertical().xy(1, 2).len(2).build();
		controller.startup(board);

		deepEqual(view.state, board.blocks());
	});

	test('Vertical block build', function() {
		var block = new WBoard().blockBuilder().vertical().xy(1, 2).len(2).build();

		deepEqual({
			x : 1,
			y : 2,
			direction : 'vertical',
			len : 2
		}, block._state);
	});

	test('Horizontal block build', function() {
		var block = new WBoard().blockBuilder().horizontal().xy(1, 2).len(2).build();

		deepEqual({
			x : 1,
			y : 2,
			direction : 'horizontal',
			len : 2
		}, block._state);
	});

	test('Vertical block move', function() {
		var block = new WBoard().blockBuilder().vertical().xy(1, 2).len(2).build();

		block.move(+1);
		equal(3, block._state.y);

		block.move(-1);
		equal(2, block._state.y);
	});

	test('Horizontal block move', function() {
		var block = new WBoard().blockBuilder().horizontal().xy(1, 2).len(2).build();

		block.move(+1);
		equal(2, block._state.x);

		block.move(-1);
		equal(1, block._state.x);
	});

	test('Horizontal move out of bounds', function() {
		var block = new WBoard().blockBuilder().horizontal().xy(4, 2).len(2).build();

		block.move(+1);
		equal(4, block._state.x);

		block.move(-5);
		equal(4, block._state.x);
	});

	test('Vertical move out of bounds', function() {
		var block = new WBoard().blockBuilder().vertical().xy(3, 0).len(2).build();

		block.move(-1);
		equal(0, block._state.y);

		block.move(+5);
		equal(0, block._state.y);
	});

	test('Vertical block intersect', function() {
		var block = new WBoard().blockBuilder().vertical().xy(2, 2).len(3).build();

		ok(!block.intersect(2, 0));
		ok(!block.intersect(2, 1));
		ok(block.intersect(2, 2));
		ok(block.intersect(2, 3));
		ok(block.intersect(2, 4));
		ok(!block.intersect(2, 5));

		ok(!block.intersect(1, 2));
	});

	test('Horizontal block intersect', function() {
		var block = new WBoard().blockBuilder().horizontal().xy(2, 2).len(3).build();

		ok(!block.intersect(0, 2));
		ok(!block.intersect(1, 2));
		ok(block.intersect(2, 2));
		ok(block.intersect(3, 2));
		ok(block.intersect(4, 2));
		ok(!block.intersect(5, 2));

		ok(!block.intersect(2, 1));
	});

	test('4x3 board out horizontal of bounds move', function() {
		var block = new WBoard(4, 3).blockBuilder().horizontal().xy(2, 0).len(2).build();

		block.move(+1);
		equal(block._state.x, 2);

		block.move(-1);
		equal(block._state.x, 1);
	});

	test('4x3 board out vertical of bounds move', function() {
		var block = new WBoard(4, 3).blockBuilder().vertical().xy(0, 1).len(2).build();

		block.move(+1);
		equal(block._state.y, 1);

		block.move(-1);
		equal(block._state.y, 0);
	});

	test('Legal move towars bounds', function() {
		var block = new WBoard(3, 3).blockBuilder().horizontal().xy(0, 0).len(2).build();

		block.move(+1);

		equal(block._state.x, 1);
	});

	test('Vetical collision test', function() {
		// Test board configuration
		// ___
		// _HH
		// _V_
		// _V_
		var board = new WBoard(3, 4);
		var vblock = board.blockBuilder().vertical().xy(1, 2).len(2).build();
		board.blockBuilder().horizontal().xy(1, 1).len(2).build();

		var controller = new WwallsController(new TestView());

		controller.startup(board);
		vblock.move(-1);

		equal(vblock._state.y, 2);
	});

	test('Horizontal collision test', function() {
		// Test board configuration
		// ____
		// __V_
		// HHV_
		// __V_
		var board = new WBoard(4, 4);
		board.blockBuilder().vertical().xy(2, 1).len(3).build();
		var hblock = board.blockBuilder().horizontal().xy(0, 2).len(2).build();

		var controller = new WwallsController(new TestView());

		controller.startup(board);
		hblock.move(+1);

		equal(hblock._state.x, 0);
	});

	test('Legal move test', function() {
		// Test board configuration
		// _HH
		// V__
		// V__
		var board = new WBoard(3, 3);
		var block = board.blockBuilder().vertical().xy(0, 1).len(2).build();
		board.blockBuilder().horizontal().xy(1, 0).len(2).build();

		var controller = new WwallsController(new TestView());

		controller.startup(board);
		block.move(-1);

		equal(block._state.y, 0);
	});

	test('Solution', function() {
		var board = new WBoard(6, 6);
		var block = board.blockBuilder().horizontal().xy(3, 2).len(2).build();

		var view = new TestView();
		var controller = new WwallsController(view);

		controller.startup(board);
		block.move(+1);

		ok(view.solved);
	});

	function TestView() {
	}

	TestView.prototype.init = function(controller) {
		this.controller = controller;
	};

	TestView.prototype.update = function(state) {
		this.state = state;
	};

	TestView.prototype.onReset = function() {
		this.onResetCalled = true;
	};

	TestView.prototype.onSolved = function() {
		this.solved = true;
	};

}());